package com.linsir.file.config;

import com.linsir.core.config.BaseConfig;
import com.linsir.core.constant.CommonConstant;
import com.linsir.core.util.S;
import com.linsir.core.util.V;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
//import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

/**
 * @author ：linsir
 * @date ：Created in 2022/9/18 22:34
 * @description：启动注册类
 * @modified By：
 * @version:
 */
@Configuration
@ComponentScan(basePackages = {"com.linsir.file"})
@MapperScan(basePackages = {"com.linsir.file.mapper"})
public class FileAutoConfig {

    @Value(value = "${linsir.file.max_size}")
    private String maxSize;


    // TODO 升级 上传内容
    /*@Bean
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver bean = new  CommonsMultipartResolver();

        bean.setDefaultEncoding(CommonConstant.CHARSET_UTF8);

        Long maxUploadSize = null;
        // 兼容 servlet 配置参数
        String servletMaxUploadSize = BaseConfig.getProperty("spring.servlet.multipart.max-request-size");
        if(V.notEmpty(servletMaxUploadSize)){
            if(S.containsIgnoreCase(servletMaxUploadSize, "M")){
                int index = S.indexOfIgnoreCase(servletMaxUploadSize,"M");
                maxUploadSize = Long.parseLong(S.substring(servletMaxUploadSize, 0, index));
                maxUploadSize = maxUploadSize * 1024 * 1024;
            }
            else if(S.containsIgnoreCase(servletMaxUploadSize, "K")){
                int index = S.indexOfIgnoreCase(servletMaxUploadSize,"K");
                maxUploadSize = Long.parseLong(S.substring(servletMaxUploadSize, 0, index));
                maxUploadSize = maxUploadSize * 1024;
            }
            else{
                maxUploadSize = Long.parseLong(servletMaxUploadSize);
            }
        }
        else{
            if (S.containsIgnoreCase(maxSize, "M")){
                int index = S.indexOfIgnoreCase(maxSize,"M");
                maxUploadSize = Long.parseLong(S.substring(maxSize, 0, index));
                maxUploadSize = maxUploadSize * 1024 * 1024;
            }
        }
        bean.setMaxUploadSize(maxUploadSize);
        return bean;
    }*/


}
